#!/bin/bash
# VimL虚拟机安装脚本

INSTALL_PREFIX="${1:-/usr/local}"
BINARY_NAME="vimlc"
BUILD_DIR="build_release"

echo "VimL虚拟机安装"
echo "=============="
echo ""
echo "安装目录: $INSTALL_PREFIX"
echo "二进制文件: $BINARY_NAME"
echo ""

# 检查Release版本是否存在
if [ ! -f "$BUILD_DIR/bin/$BINARY_NAME" ]; then
    echo "错误: Release版本未找到"
    echo "请先运行 './build_release.sh' 构建Release版本"
    exit 1
fi

# 检查权限
if [ ! -w "$INSTALL_PREFIX/bin" ]; then
    echo "错误: 没有写入权限到 $INSTALL_PREFIX/bin"
    echo "请使用 sudo 运行此脚本或选择其他安装目录"
    exit 1
fi

# 创建安装目录
mkdir -p "$INSTALL_PREFIX/bin"

# 复制二进制文件
echo "安装 $BINARY_NAME 到 $INSTALL_PREFIX/bin/"
cp "$BUILD_DIR/bin/$BINARY_NAME" "$INSTALL_PREFIX/bin/"
chmod +x "$INSTALL_PREFIX/bin/$BINARY_NAME"

# 验证安装
if [ -x "$INSTALL_PREFIX/bin/$BINARY_NAME" ]; then
    echo "安装成功!"
    echo ""
    echo "使用方法:"
    echo "  $BINARY_NAME <script.vim>          # 运行VimL脚本"
    echo "  $BINARY_NAME -d <script.vim>       # 调试模式运行"
    echo "  $BINARY_NAME --help                # 显示帮助"
    echo ""
    echo "示例:"
    echo "  $BINARY_NAME example/recursive_fibonaccy.vim"
else
    echo "安装失败!"
    exit 1
fi